<?
class Image
{
   var $gd_im=NULL;    // a gd image oject
   var $mime_type;
   var $file_type;
   var $error_msg;

   function Image($file_name='')
   {
      global $_text;
      $this->file_type='';
      $this->gd_im=NULL;
      if (empty($file_name)){
         return false;
      }
      $image_info = @getImageSize($file_name) ; // see EXIF for faster way
      if (!$image_info){
          return false;
      }
      $this->mime_type=$image_info['mime'];
      switch ($image_info['mime'])
      {
          case 'image/gif':
              $this->file_type='gif';
              if (imagetypes() & IMG_GIF)  { // not the same as IMAGETYPE
                  $this->gd_im = imageCreateFromGIF($file_name) ;
              } else {
                  $this->error_msg = sprintf($_text['image_type_X_not_supported'],'GIF');
              }
              break;
          case 'image/jpeg':
              $this->file_type='jpg';
              if (imagetypes() & IMG_JPG)  {
                  $this->gd_im = imageCreateFromJPEG($file_name) ;
				  // imageJPEG($this->gd_im,'test.jpg',75);
              } else {
                  $this->error_msg = sprintf($_text['image_type_X_not_supported'],'JPG');
              }
              break;
          case 'image/png':
              $this->file_type='png';
              if (imagetypes() & IMG_PNG)  {
                  $this->gd_im = imageCreateFromPNG($file_name) ;
              } else {
                  $this->error_msg = sprintf($_text['image_type_X_not_supported'],'PNG');
              }
              break;
          case 'image/wbmp':
              $this->file_type='bmp';
              if (imagetypes() & IMG_WBMP)  {
                  $this->gd_im = imageCreateFromWBMP($file_name) ;
              } else {
                  $this->error_msg = sprintf($_text['image_type_X_not_supported'],'WBMP');
              }
              break;
          default:
              $this->error_msg = sprintf($_text['image_type_X_not_supported'],$image_info['mime']);
              return false;
              break;
      }

      $this->width = imagesx($this->gd_im) ;
      $this->height = imagesy($this->gd_im) ;
      return $this->gd_im;
   }

   function destroy()
   {
      return imageDestroy($this->gd_im);
   }

   function resize($out_file, $t_wd = 120,$t_ht = 90)
   {
      if ($this->gd_im!=NULL)
      {
         $o_wd = imagesx($this->gd_im) ;
         $o_ht = imagesy($this->gd_im) ;

         if($o_wd/$o_ht>$t_wd/$t_ht)
         {// thumbnail width = target * original width / original height
            $t_ht = round($o_ht * $t_wd / $o_wd) ;
            $t_im = imageCreateTrueColor($t_wd,$t_ht);
         }
         else
         {// thumbnail height = target * original height / original witdh
            $t_wd = round($o_wd * $t_ht / $o_ht) ;
            $t_im = imageCreateTrueColor($t_wd,$t_ht);
         }
         imageCopyResampled($t_im, $this->gd_im, 0, 0, 0, 0, $t_wd, $t_ht, $o_wd, $o_ht);
         imageJPEG($t_im,$out_file,75);
         imageDestroy($t_im);
         return true;
      }
      else
      {
         return false;
      }
   }

   function create_avatar($out_file, $t_wd = 100,$t_ht = 100)
   {
      if ($this->gd_im!=NULL)
      {
         //echo "<br>debug:",$t_wd,':',$t_ht;
         $o_wd = $this->width ;
         $o_ht = $this->height ;
	 	 // echo "<br>debug: o_wd $o_wd o_ht $o_ht ";
         if($o_wd/$o_ht>$t_wd/$t_ht)
         {// copy width = copy height * avatar width / avatar height
            $copy_ht=$o_ht;
            $copy_wd = round($copy_ht * $t_wd / $t_ht) ;
            $source_x=round(($o_wd-$copy_wd)/2);
            $source_y=0;
         }
         else
         {// copy height= copy width * avatar height / avatar width
            $copy_wd=$o_wd;
            $copy_ht = round($copy_wd * $t_ht / $t_wd) ;
            $source_x=0;
            $source_y=round(($o_ht-$copy_ht)/2);;
         }
         //echo " s: $source_x, $source_y, o: $o_wd, $o_ht, c: $copy_wd, $copy_ht";
         $t_im = imageCreateTrueColor($t_wd,$t_ht);
		 //echo "<br>: $source_x, $source_y , $copy_wd, $copy_ht";
         imageCopyResampled($t_im, $this->gd_im, 0, 0, $source_x, $source_y,
                                   $t_wd, $t_ht, $copy_wd, $copy_ht);
         imageJPEG($t_im,$out_file,75);
         imageDestroy($t_im);
         return true;
      }
      else
      {
         return false;
      }
  }
}
?>